package engine.logic
{
	import com.greensock.TimelineMax;
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Point;
	import newx.com.ResManager;
	import newx.utils.BitmapAPI;
	
	/**
	 * @author gf
	 */	
	
	public class ShowDamageNum extends Sprite
	{
		private var _bitMap	:Bitmap		=	new Bitmap();
		
		private var _isUse		:Boolean	=	false;
		
		private var _pos		:Point		=	new Point();
		
		private var _width		:int;
		
		private var _timeline	:TimelineMax;
		
		private static var attackDamages	:Array	=	[];
		
		private static var _numObj		:Object	=	{};
		
		private static var numHeight:Number	=	50;
		
		public function ShowDamageNum()
		{	
			addChild(_bitMap);
			mouseChildren = mouseEnabled = false;
		}
		
		private static function getFreeDamageEffect():ShowDamageNum
		{
			var showDamage : ShowDamageNum;
			for each( var effect : ShowDamageNum in attackDamages )
			{
				if(!effect._isUse)
				{
					effect._isUse	=	true;
					effect.clear();
					
					showDamage =  effect;
					break;
				}
			}
			
			if( null == showDamage )
			{
				showDamage = new ShowDamageNum();
				attackDamages.push( showDamage);
			}
			
			showDamage._isUse	=	true;
			showDamage.clear();
			
			return showDamage;
		}
		
		public static function ShowDamageEffect(data : int, p : Point ,display:DisplayObjectContainer,effect:int=0, height:Number=50 ):void
		{
			numHeight	=	height;
			
			var tDamge : ShowDamageNum =	getFreeDamageEffect();
			
			//			if( data > 0 )
			//			{
			tDamge.showNum(data,effect);
			//			}
			
			show( tDamge, p ,display,effect);
		}
		
		private static function show( tDamge : ShowDamageNum, p : Point , display:DisplayObjectContainer, effect:int=0):void
		{
			tDamge.y	=	p.y;
			tDamge.x 	= 	p.x - tDamge._width * .5;
			
			display.addChild(tDamge);
			
			tDamge.playerEffect(effect);
		}
		
		private function showNum(data : int,effect:int=0 ):void
		{
			var dataArr	:Array	=	String( Math.abs( data ) ).split("");
			var leng	:int	=	dataArr.length;
			var type	:String;
			if(data>0)
			{
				type="14";
			}
			else
			{
				type="15";
			}
			var bmd		:BitmapData = getRes( type + dataArr[ 0 ] );
			if(effect!=0)
			{
				var sign:String="加血";
				//符号
				var signBmd:BitmapData = getRes( sign );
				_bitMap.bitmapData = new BitmapData( (bmd.width + 2 ) * leng + 30 + signBmd.width, bmd.height + 20, true, 0);
				
				_pos.x	= 0;
				_pos.y	= ( bmd.height - signBmd.height ) * .5;
				_bitMap.bitmapData.copyPixels( signBmd, _bitMap.bitmapData.rect, _pos );
				_pos.x	=	signBmd.width + 2;
				_pos.y	=	0;
				_bitMap.bitmapData.copyPixels( bmd, _bitMap.bitmapData.rect, _pos );
			}
			else
			{
				_pos.x	=	0;
				_bitMap.bitmapData = new BitmapData( (bmd.width + 10) * leng, bmd.height + 20, true, 0 );
				_bitMap.bitmapData.copyPixels( bmd, _bitMap.bitmapData.rect, _pos );
			}
			
			
			
			for( var i : int = 1; i < leng; i++ )
			{
				_pos.x += bmd.width + i + 1;
				bmd = getRes( type + dataArr[i] );
				
				_bitMap.bitmapData.copyPixels( bmd, bmd.rect, _pos );
			}
			_width = _bitMap.width;
		}
		
		public function clear():void
		{
			if(_bitMap.bitmapData != null)
			{
				_bitMap.bitmapData.dispose();
			}
		}
		
		private function getRes(key : String):BitmapData
		{
			if( null != _numObj[ key ] )
			{
				return _numObj[ key ]
			}
			var pic : BitmapData = BitmapAPI.getBitmapData( ResManager.newResClass( "fig" + key ) );
			_numObj[ key ] = pic;
			return pic;
		}
		
		private function playerEffect(effect:int=0):void
		{
			alpha	=	0;
			scaleX	=	0;
			scaleY	=	0;
			
			_timeline		=	new TimelineMax();
			if(effect ==0)
			{
				_timeline.append(new TweenLite(this, 1, { y:y-numHeight, scaleX:1, scaleY:1, alpha:1}));
				_timeline.append(new TweenLite(this, 0.5, { delay:.2, alpha:0, x:x+_width*.15, scaleX:.5, scaleY:.5, onComplete:onDisappear}));
			}
			else if(effect ==1)
			{
				_timeline.append(new TweenLite(this, .2, { y:y-100, x:x-_width * .5, scaleX:1, scaleY:1, alpha:1}));
				_timeline.append(new TweenLite(this, .3, { delay:.2, alpha:0, x:x+_width*.06, scaleX:.7, scaleY:.7, onComplete:onDisappear}));
			}
			else if(effect ==2)
			{
				_timeline.append(new TweenLite(this, .2, { y:y-120, x:x-_width * .5, scaleX:2, scaleY:2, alpha:1}));
				_timeline.append(new TweenLite(this, .3, { delay:.2, alpha:0, x:x+_width*.06, scaleX:.7, scaleY:.7, onComplete:onDisappear}));
			}
			
			_timeline.play();
		}
		
		private function onDisappear():void
		{
			if(_timeline != null)
			{
				_timeline.clear();
				_timeline.kill();
			}
			
			if(this.parent!=null)
			{
				parent.removeChild(this);
			}
			
			_isUse	=	false;
		}
	}
}